<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>AngelScript: Registering a value type</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   
   </div>
   
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_register_val_type.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Registering a value type </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p>When registering a value type, the size of the type must be given so that AngelScript knows how much space is needed for it. If the type doesn't require any special treatment, i.e. doesn't contain any pointers or other resource references that must be maintained, then the type can be registered with the flag <a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa8ad017ddf25368870b28ee0fba96495a">asOBJ_POD</a>. In this case AngelScript doesn't require the default constructor, assignment behaviour, or destructor as it will be able to automatically handle these cases the same way it handles built-in primitives.</p>
<p>If you plan on passing or returning the type by value to registered functions that uses native calling convention, you also need to inform <a class="el" href="doc_register_val_type.html#doc_reg_val_2">how the type is implemented in the application</a>, but if you only plan on using generic calling conventions, or don't pass these types by value then you don't need to worry about that.</p>
<div class="fragment"><pre class="fragment"><span class="comment">// Register a primitive type, that doesn&#39;t need any special management of the content</span>
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a29c6c087c8c5b5cdb6271cfd161cc5a6" title="Registers a new object type.">RegisterObjectType</a>(<span class="stringliteral">&quot;pod&quot;</span>, <span class="keyword">sizeof</span>(pod), <a class="code" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa9fc16a8ac0f30f9ff9c6568e0b7be91d" title="A value type.">asOBJ_VALUE</a> | <a class="code" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa8ad017ddf25368870b28ee0fba96495a" title="A plain-old-data type. Only valid for value types.">asOBJ_POD</a>); assert( r &gt;= 0 );

<span class="comment">// Register a class that must be properly initialized and uninitialized</span>
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a29c6c087c8c5b5cdb6271cfd161cc5a6" title="Registers a new object type.">RegisterObjectType</a>(<span class="stringliteral">&quot;val&quot;</span>, <span class="keyword">sizeof</span>(val), <a class="code" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa9fc16a8ac0f30f9ff9c6568e0b7be91d" title="A value type.">asOBJ_VALUE</a>); assert( r &gt;= 0 );
</pre></div><dl class="see"><dt><b>See also:</b></dt><dd>The <a class="el" href="doc_addon_std_string.html">string object</a> or the <a class="el" href="doc_addon_math.html">complex type in the math add-on</a> for examples of value types </dd>
<dd>
<a class="el" href="doc_adv_generic_handle.html">Registering a generic handle type</a> for a more specific example of a value type</dd></dl>
<h2><a class="anchor" id="doc_reg_val_1"></a>
Constructor and destructor</h2>
<p>If a constructor or destructor is needed they shall be registered the following way:</p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> Constructor(<span class="keywordtype">void</span> *memory)
{
  <span class="comment">// Initialize the pre-allocated memory by calling the</span>
  <span class="comment">// object constructor with the placement-new operator</span>
  <span class="keyword">new</span>(memory) Object();
}

<span class="keywordtype">void</span> Destructor(<span class="keywordtype">void</span> *memory)
{
  <span class="comment">// Uninitialize the memory by calling the object destructor</span>
  ((Object*)memory)-&gt;~Object();
}

<span class="comment">// Register the behaviours</span>
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;val&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5aa4cf235bfbf72ec03d0f651cea324101" title="Constructor.">asBEHAVE_CONSTRUCT</a>, <span class="stringliteral">&quot;void f()&quot;</span>, <a class="code" href="angelscript_8h.html#a78f8f2c7f1c88b12e74a5ac47b4184ae" title="Returns an asSFuncPtr representing the function specified by the name.">asFUNCTION</a>(Constructor), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4ac08652c72f1cc0dc81c37812fab0e253" title="A cdecl function that takes the object pointer as the last parameter.">asCALL_CDECL_OBJLAST</a>); assert( r &gt;= 0 );
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;val&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5a0748a0f3a559354761ce15c2d1de2e51" title="Destructor.">asBEHAVE_DESTRUCT</a>, <span class="stringliteral">&quot;void f()&quot;</span>, <a class="code" href="angelscript_8h.html#a78f8f2c7f1c88b12e74a5ac47b4184ae" title="Returns an asSFuncPtr representing the function specified by the name.">asFUNCTION</a>(Destructor), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4ac08652c72f1cc0dc81c37812fab0e253" title="A cdecl function that takes the object pointer as the last parameter.">asCALL_CDECL_OBJLAST</a>); assert( r &gt;= 0 );
</pre></div><p>Note that you may need to include the &lt;new&gt; header to declare the placement new operator that is used to initialize a preallocated memory block.</p>
<h2><a class="anchor" id="doc_reg_val_2"></a>
Value types and native calling conventions</h2>
<p>If the type will be passed to and from the application by value using native calling conventions, it is important to inform AngelScript of its real type in C++, otherwise AngelScript won't be able to determine exactly how C++ is treating the type in a parameter or return value.</p>
<p>There are a few different flags for this:</p>
<table  border="0" cellspacing="0" cellpadding="0">
<tr>
<td><a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa103297ed88696a3c30ec12e533d902c3">asOBJ_APP_CLASS</a> &#160; </td><td>The C++ type is a class, struct, or union </td></tr>
<tr>
<td><a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aafd799c0705cee720a12ceb2838796024">asOBJ_APP_CLASS_CONSTRUCTOR</a> &#160; </td><td>The C++ type has a defined constructor </td></tr>
<tr>
<td><a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa18d80c6d92e4bc104955da393c966917">asOBJ_APP_CLASS_DESTRUCTOR</a> &#160; </td><td>The C++ type has a defined destructor </td></tr>
<tr>
<td><a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa6bf9b7bead31a40e7983538d8cecc3a4">asOBJ_APP_CLASS_ASSIGNMENT</a> &#160; </td><td>The C++ type has a defined assignment operator </td></tr>
<tr>
<td><a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa925febfd30b150d97a84b7c6ee6a8677">asOBJ_APP_CLASS_COPY_CONSTRUCTOR</a> &#160; </td><td>The C++ type has a defined copy constructor </td></tr>
<tr>
<td><a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa539ede421d313b03464c88cb15f08c75">asOBJ_APP_PRIMITIVE</a> &#160; </td><td>The C++ type is a C++ primitive, but not a float or double </td></tr>
<tr>
<td><a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa7f7690d53d9bfc580e09ac7bf5868175">asOBJ_APP_FLOAT</a> &#160; </td><td>The C++ type is a float or double </td></tr>
</table>
<p>Note that these don't represent how the type will behave in the script language, only what the real type is in the host application. So if you want to register a C++ class that you want to behave as a primitive type in the script language you should still use the flag <a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa103297ed88696a3c30ec12e533d902c3">asOBJ_APP_CLASS</a>. The same thing for the flags to identify that the class has a constructor, destructor, assignment operator, or copy constructor. These flags tell AngelScript that the class has the respective function, but not that the type in the script language should have these behaviours.</p>
<p>For class types there is also a shorter form of the flags for each combination of the 5 flags. They are of the form <a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa12d358962300537f2b0da20106eb270c">asOBJ_APP_CLASS_CDAK</a>, where the existance of the last letters determine if the constructor, destructor, and/or assignment behaviour are available. For example <a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa12d358962300537f2b0da20106eb270c">asOBJ_APP_CLASS_CDAK</a> is defined as <a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa103297ed88696a3c30ec12e533d902c3">asOBJ_APP_CLASS</a> | <a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aafd799c0705cee720a12ceb2838796024">asOBJ_APP_CLASS_CONSTRUCTOR</a> | <a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa18d80c6d92e4bc104955da393c966917">asOBJ_APP_CLASS_DESTRUCTOR</a> | <a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa6bf9b7bead31a40e7983538d8cecc3a4">asOBJ_APP_CLASS_ASSIGNMENT</a> | <a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa925febfd30b150d97a84b7c6ee6a8677">asOBJ_APP_CLASS_COPY_CONSTRUCTOR</a>.</p>
<div class="fragment"><pre class="fragment"><span class="comment">// Register a complex type that will be passed by value to the application</span>
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a29c6c087c8c5b5cdb6271cfd161cc5a6" title="Registers a new object type.">RegisterObjectType</a>(<span class="stringliteral">&quot;complex&quot;</span>, <span class="keyword">sizeof</span>(complex), <a class="code" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa9fc16a8ac0f30f9ff9c6568e0b7be91d" title="A value type.">asOBJ_VALUE</a> | <a class="code" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa12d358962300537f2b0da20106eb270c" title="The C++ type is a class with a constructor, destructor, assignment operator, and copy constructor...">asOBJ_APP_CLASS_CDAK</a>); assert( r &gt;= 0 );
</pre></div><p>Make sure you inform these flags correctly, because if you do not you may get various errors when executing the scripts. Common problems are stack corruptions and invalid memory accesses. In some cases you may face more silent errors that may be difficult to detect, e.g. the function is not returning the expected values.</p>
<p>On some platforms the native calling convention may require further knowledge about the class members in order to work properly; most notable are the Linux 64bit and Mac OSX 64bit systems with the GNUC compiler. On these systems small classes that do not have a destructor or a copy constructor will have different behaviours depending on the type of their members.</p>
<p>AngelScript lets the application inform the two most common variants, i.e. the class should be treated as if all members are integers, or it should be treated as if all members are floats.</p>
<table  border="0" cellspacing="0" cellpadding="0">
<tr>
<td><a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa5b8de58c5be3145aaa3e54008fb2edeb">asOBJ_APP_CLASS_ALLINTS</a> &#160; </td><td>The C++ class members can be treated as if all integers </td></tr>
<tr>
<td><a class="el" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa12afb6a0fa4ac874ce89815d3611823d">asOBJ_APP_CLASS_ALLFLOATS</a> &#160; </td><td>The C++ class members can be treated as if all floats or doubles </td></tr>
</table>
<p>It is difficult to explain when one or the other should be used as it requires indepth knowledge of the ABI for the respective system, so if you find that you really need to use these flags, make sure you perform adequate testing to guarantee that your functions are called correctly by the script engine. If neither of these flags work, and you're not able to change the class to work without them, then the only other option is to use the generic calling convention, preferably with the <a class="el" href="doc_addon_autowrap.html">auto wrappers</a>. </p>
</div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>

    <li class="footer">Generated on Sun Jan 29 2012 15:41:04 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
